* CPS_proc.do
* 2014.02.05
* Last update 2014.10.22: added 2013 data
* Processes geographic identifiers from basic CPS for link to ATUS 

capture log close
set more off
timer clear 1
timer on 1
clear
set matsize 10000
local work "/DIRECTORY"
log using "`work'/logs/CPS_proc.log", replace


***********************
* Aug 02 - Dec 02
* match on HRHHID PULINENO HRMONTH HRYEAR4 HRSERSUF
***********************

foreach mo in aug sep oct nov dec {
	infix double HRHHID 1-15 HRMONTH 16-17 HRYEAR4 18-21 str HRSERSUF 75-76 PULINENO 147-148 ///
		str GESTFIPS 93-94 str GTCMSA 95-96 str GTMSA 97-100 str GTCO 101-103 GTMSAST 104 GEMETSTA 105 GTMSASZ 107 GECMSASZ 108 ///
		PEHRUSL1 218-219 PEHRFTPT 222-223 PUHROT1 239-240 PEHRACT1 243-244 PRHRUSL 399-400 ///
		PEERNUOT 500-501 PEERNPER 502-503 PEERNRT 504-505 PEERNHRY 506-507 PTERNH1C 508-511 PTERNH2 512-515 PTERNH1O 516-519 PTERNHLY 520-523 PTHR 524 PEERNHRO 525-526 ///
		PTERNWA 527-534 PTWK 535 PTERN 540-547 PTERN2 548-555 PTOT 556 PEERNWKP 559-560 PEERNLAB 561-562 PEERNCOV 563-564 ///
		using "`work'/CPS/`mo'02pub.cps", clear
	rename GTCMSA CMSA
	rename GTMSA MSA
	rename GTCO COUNTY
	rename GTMSAST msa_status
	rename GEMETSTA met_status
	rename GTMSASZ msa_pmsa_size
	rename GECMSASZ cmsa_msa_size
	rename PTERNH1C hrly_wage
	rename PTERNH2 hrly_wage2
	rename PTERNH1O hrly_wage_out
	rename PTERNHLY hrly_recode
	rename PTERNWA wkly_earnings
	rename PTERN wkly_ot_earnings
	rename PTERN2 wkly_ot_earn_calc
	save "`work'/CPS/`mo'02.dta", replace
}

***********************
* Jan 03 - Apr 04
* match on HRHHID PULINENO HRMONTH HRYEAR4 HRSERSUF
***********************
foreach mo in jan03 feb03 mar03 apr03 may03 jun03 jul03 aug03 sep03 oct03 nov03 dec03 jan04 feb04 mar04 apr04 {
	infix double HRHHID 1-15 PULINENO 147-148 HRMONTH 16-17 HRYEAR4 18-21 str HRSERSUF 75-76 ///
		str GESTFIPS 93-94 str GECMSA 95-96 str GEMSA 97-100 str GECO 101-103 GEMSAST 104 GEMETSTA 105 GEMSASZ 107 GECMSASZ 108 ///
		PEHRUSL1 218-219 PEHRFTPT 222-223 PUHROT1 239-240 PEHRACT1 243-244 PRHRUSL 399-400 ///
		PEERNUOT 500-501 PEERNPER 502-503 PEERNRT 504-505 PEERNHRY 506-507 PUERNH1C 508-511 PEERNH2 512-515 PEERNH1O 516-519 PRERNHLY 520-523 PTHR 524 PEERNHRO 525-526 ///
		PRERNWA 527-534 PTWK 535 PEERN 540-547 PUERN2 548-555 PTOT 556 PEERNWKP 559-560 PEERNLAB 561-562 PEERNCOV 563-564 ///
		using "`work'/CPS/`mo'pub.cps", clear
	rename GECMSA CMSA
	rename GEMSA MSA
	rename GECO COUNTY
	rename GEMSAST msa_status
	rename GEMETSTA met_status
	rename GEMSASZ msa_pmsa_size
	rename GECMSASZ cmsa_msa_size
	rename PUERNH1C hrly_wage
	rename PEERNH2 hrly_wage2
	rename PEERNH1O hrly_wage_out
	rename PRERNHLY hrly_recode
	rename PRERNWA wkly_earnings
	rename PEERN wkly_ot_earnings
	rename PUERN2 wkly_ot_earn_calc
	save "`work'/CPS/`mo'.dta", replace
}

***********************
* May 04 - July 05
* match on HRHHID PULINENO HRMONTH HRYEAR4 HRHHID2 here and below
***********************
foreach mo in may04 jun04 jul04 aug04 sep04 oct04 nov04 dec04 jan05 feb05 mar05 apr05 may05 jun05 jul05 {
	infix double HRHHID 1-15 PULINENO 147-148 HRMONTH 16-17 HRYEAR4 18-21 HRHHID2 71-75 ///
		str GESTFIPS 93-94 str GTCBSA 96-100 str GTCO 101-103 GTCBSAST 104 GTMETSTA 105 GTCBSASZ 107 str GTCSA 108-110 ///
		PEHRUSL1 218-219 PEHRFTPT 222-223 PUHROT1 239-240 PEHRACT1 243-244 PRHRUSL 399-400 ///
		PEERNUOT 500-501 PEERNPER 502-503 PEERNRT 504-505 PEERNHRY 506-507 PUERNH1C 508-511 PEERNH2 512-515 PEERNH1O 516-519 PRERNHLY 520-523 PTHR 524 PEERNHRO 525-526 ///
		PRERNWA 527-534 PTWK 535 PEERN 540-547 PUERN2 548-555 PTOT 556 PEERNWKP 559-560 PEERNLAB 561-562 PEERNCOV 563-564 ///
		using "`work'/CPS/`mo'pub.cps", clear
	rename GTCBSA CBSA
	rename GTCSA CSA
	rename GTCO COUNTY
	rename GTMETSTA met_status
	rename PUERNH1C hrly_wage
	rename PEERNH2 hrly_wage2
	rename PEERNH1O hrly_wage_out
	rename PRERNHLY hrly_recode
	rename PRERNWA wkly_earnings
	rename PEERN wkly_ot_earnings
	rename PUERN2 wkly_ot_earn_calc
	save "`work'/CPS/`mo'.dta", replace
}

***********************
* Aug 05 - Dec 06
***********************
foreach mo in aug05 sep05 oct05 nov05 dec05 jan06 feb06 mar06 apr06 may06 jun06 jul06 aug06 sep06 oct06 nov06 dec06 {
	if inlist("`mo'", "aug05", "sep05", "oct05", "nov05", "dec05", "jan06", "feb06") {
		local suff "cps"
	}
	else {
		local suff "dat"
	}
	infix double HRHHID 1-15 PULINENO 147-148 HRMONTH 16-17 HRYEAR4 18-21 HRHHID2 71-75 ///
		str GESTFIPS 93-94 str GTCBSA 96-100 str GTCO 101-103 GTCBSAST 104 GTMETSTA 105 GTCBSASZ 107 str GTCSA 108-110 ///
		PEHRUSL1 218-219 PEHRFTPT 222-223 PUHROT1 239-240 PEHRACT1 243-244 PRHRUSL 399-400 ///
		PEERNUOT 500-501 PEERNPER 502-503 PEERNRT 504-505 PEERNHRY 506-507 PUERNH1C 508-511 PEERNH2 512-515 PEERNH1O 516-519 PRERNHLY 520-523 PTHR 524 PEERNHRO 525-526 ///
		PRERNWA 527-534 PTWK 535 PEERN 540-547 PUERN2 548-555 PTOT 556 PEERNWKP 559-560 PEERNLAB 561-562 PEERNCOV 563-564 ///
		using "`work'/CPS/`mo'pub.`suff'", clear
	rename GTCBSA CBSA
	rename GTCSA CSA
	rename GTCO COUNTY
	rename GTMETSTA met_status
	rename PUERNH1C hrly_wage
	rename PEERNH2 hrly_wage2
	rename PEERNH1O hrly_wage_out
	rename PRERNHLY hrly_recode
	rename PRERNWA wkly_earnings
	rename PEERN wkly_ot_earnings
	rename PUERN2 wkly_ot_earn_calc
	save "`work'/CPS/`mo'.dta", replace
}

***********************
* Jan 07 - Dec 13
***********************
foreach mo in jan07 feb07 mar07 apr07 may07 jun07 jul07 aug07 sep07 oct07 nov07 dec07 ///
	jan08 feb08 mar08 apr08 may08 jun08 jul08 aug08 sep08 oct08 nov08 dec08 ///
	jan09 feb09 mar09 apr09 may09 jun09 jul09 aug09 sep09 oct09 nov09 dec09 ///
	jan10 feb10 mar10 apr10 may10 jun10 jul10 aug10 sep10 oct10 nov10 dec10 ///
	jan11 feb11 mar11 apr11 may11 jun11 jul11 aug11 sep11 oct11 nov11 dec11 ///
	jan12 feb12 mar12 apr12 may12 jun12 jul12 aug12 sep12 oct12 nov12 dec12 ///
	jan13 feb13 mar13 apr13 may13 jun13 jul13 aug13 sep13 oct13 nov13 dec13 {
	infix double HRHHID 1-15 PULINENO 147-148 HRMONTH 16-17 HRYEAR4 18-21 HRHHID2 71-75 ///
		str GESTFIPS 93-94 str GTCBSA 96-100 str GTCO 101-103 GTCBSAST 104 GTMETSTA 105 GTCBSASZ 107 str GTCSA 108-110 ///
		PEHRUSL1 218-219 PEHRFTPT 222-223 PUHROT1 239-240 PEHRACT1 243-244 PRHRUSL 399-400 ///
		PEERNUOT 500-501 PEERNPER 502-503 PEERNRT 504-505 PEERNHRY 506-507 PUERNH1C 508-511 PEERNH2 512-515 PEERNH1O 516-519 PRERNHLY 520-523 PTHR 524 PEERNHRO 525-526 ///
		PRERNWA 527-534 PTWK 535 PEERN 540-547 PUERN2 548-555 PTOT 556 PEERNWKP 559-560 PEERNLAB 561-562 PEERNCOV 563-564 ///
		using "`work'/CPS/`mo'pub.dat", clear
	rename GTCBSA CBSA
	rename GTCSA CSA
	rename GTCO COUNTY
	rename GTMETSTA met_status
	rename PUERNH1C hrly_wage
	rename PEERNH2 hrly_wage2
	rename PEERNH1O hrly_wage_out
	rename PRERNHLY hrly_recode
	rename PRERNWA wkly_earnings
	rename PEERN wkly_ot_earnings
	rename PUERN2 wkly_ot_earn_calc
	save "`work'/CPS/`mo'.dta", replace
}


***********************
* Processing
***********************
* Appending
clear
foreach mo in aug sep oct nov dec {
	append using "`work'/CPS/`mo'02.dta"
}
foreach yr in 03 04 05 06 07 08 09 10 11 12 13 {
	foreach mo in jan feb mar apr may jun jul aug sep oct nov dec {
		append using "`work'/CPS/`mo'`yr'.dta"
	}
}

* Renaming
rename HRHHID hrhhid
rename PULINENO pulineno
rename HRHHID2 hrhhid2
rename HRSERSUF hrsersuf
rename HRYEAR4 hryear4
rename HRMONTH hrmonth

* Labeling variables
label variable PEHRUSL1 "Usual hrs per wk - main job"
label variable PEHRFTPT "Full time - 35+ hr (1=yes 2=no 3=hrs vary)"
label variable PUHROT1 "OT indicator (1=yes 2=no)"
label variable PEHRACT1 "Actual hrs last wk"
label variable PRHRUSL "Usual hrs per wk (binned)"
label variable PEERNUOT "OT / tips / commissions (1=yes 2=no)"
label variable PEERNPER "Periodicity (binned)"
label variable PEERNRT "Paid hourly rate (1=yes 2=no)"
label variable PEERNHRY "Hourly worker (1=yes 2=no)"
label variable PTHR "Hourly pay topcoded (0=no 1=yes)"
label variable PEERNHRO "Usual hours"
label variable PTWK "Weekly earnings topcoded (0=no 1=yes)"
label variable PTOT "Weekly overtime amount topcoded (0=no 1=yes)"
label variable PEERNWKP "Paid weeks per year"
label variable PEERNLAB "Union / emp. assn. member (1=yes 2=no)"
label variable PEERNCOV "Union / emp. assn. contract (1=yes 2=no)"
label variable GTCBSAST "Principal city/balance status"
label variable GTCBSASZ "Metro area (CBSA) size (discrete)"

* Cleaning up county FIPS codes
gen len = length(COUNTY)
replace COUNTY = "00" + COUNTY if len==1
replace COUNTY = "0" + COUNTY if len==2
drop len

* New date variable
gen tm = ym(hryear4, hrmonth)
format tm %tm

* Handling missing values and units
replace hrsersuf = "" if hrsersuf=="-1"
replace PEHRUSL1 = . if inrange(PEHRUSL1, -4, -1)
replace PEHRFTPT = . if inrange(PEHRFTPT, -3, -1) /* Most records have a -1 here, but that is not covered in data dictionaries */
replace PUHROT1 = . if inrange(PUHROT1, -3, -1)
replace PEHRACT1 = . if PEHRACT1==-1
replace PRHRUSL = . if PRHRUSL==-1
replace PEERNUOT = . if PEERNUOT==-1
replace PEERNPER = . if PEERNPER==-1
replace PEERNRT = . if inrange(PEERNRT, -3, -1)
replace PEERNHRY = . if PEERNHRY==-1
replace PEERNHRO = . if inrange(PEERNHRO, -2, -1)
replace PEERNHRO = . if inrange(PEERNHRO, -2, -1)
replace PEERNLAB = . if PEERNLAB==-1
replace PEERNCOV = . if PEERNCOV==-1
replace hrly_wage = . if inrange(hrly_wage, -3, -1)
replace hrly_wage = hrly_wage/100 /* converting cents to dollars */
replace hrly_wage2 = . if hrly_wage2==-1
replace hrly_wage2 = hrly_wage2/100 /* converting cents to dollars */
replace hrly_wage_out = . if inrange(hrly_wage_out, -3, -1)
replace hrly_wage_out = hrly_wage_out/100 /* converting cents to dollars */
replace hrly_recode = . if inrange(hrly_recode, -3, -1)
replace hrly_recode = hrly_recode/100 /* converting cents to dollars */
replace wkly_earnings = . if wkly_earnings==-1
replace wkly_earnings = wkly_earnings/100 /* converting cents to dollars */
replace wkly_ot_earnings = . if wkly_ot_earnings==-1
replace wkly_ot_earnings = wkly_ot_earnings/100 /* converting cents to dollars */
replace wkly_ot_earn_calc = . if wkly_ot_earn_calc==-1
replace wkly_ot_earn_calc = wkly_ot_earn_calc/100 /* converting cents to dollars */


* Saving
compress
save "`work'/CPS_geo.dta", replace







timer off 1
timer list 1
capture log close


